<template>
    <div>
         <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="javascript:;">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card >
       <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear='getGoodsList'
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click='getGoodsList'
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="goaddpage"
          >
            添加商品
          </el-button>
        </el-col>
      </el-row>
      <!-- 商品展示表格 -->
  <el-table :data="goodslist"
            style="width: 100%"
            border
            >
    <el-table-column  type='index' label="#"  width="80"> 
    </el-table-column>
    <el-table-column prop="goods_name"
                     label="商品名称"
                     >
    </el-table-column>
    <el-table-column prop="goods_price"
                     label="商品价格(元)"
                     width="180">
    </el-table-column>
    <el-table-column prop="goods_number"
                     label="商品数量"
                     width="180">
    </el-table-column>
    <el-table-column prop="upd_time"
                     label="创建时间"
                     width="180" :formatter="formatterDate">
    </el-table-column>
    <el-table-column prop="goods_price"
                     label="操作"
                     width="180">
                        <template v-slot={row}>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click='goEditPage(row)'
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click='removeUserById(row)'
            ></el-button>
                        </template>
                        
    </el-table-column>
  </el-table>
      <!-- 分页模块 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    </div>
</template>
<script>
import Vue from 'vue'
export default {
  props: [],
  data () {
    return {
        queryInfo:{
            query:'',
            pagenum:1,
            pagesize:10
        },
        // 商品列表
        goodslist:[],
        // 总数据条数
        total:0,
   
    }
  },
  mounted () {
      this.getGoodsList()
  },
  methods: {
    //   获取数据列表
   async getGoodsList(){
       const { data: res } = await this.$http.get('goods',{params:this.queryInfo})
      //  console.log(res);
      if(res.meta.status !== 200){return this.message.error('获取商品列表失败')}
  this.goodslist=res.data.goods
  this.total=res.data.total
},
    // 点击添加商品跳转到商品添加页面
    goaddpage(){
        this.$router.push('/add')
    },
    // 格式化时间
    formatterDate(row){
      return Vue.filter('dateFormat')(row.add_time,'yyyy-MM-DD hh:mm:ss')
    },
    // 编辑功能
   async showEditDialog(row){
this.editDialogVisible =true
  // 发送请求通过id获取商品
  const { data: res } = await this.$http.get(`goods/${row.goods_id}`)
  // console.log(res);
  if(res.meta.status !== 200){return this.$message.error('获取商品失败')}
    this.editGoodsForm=res.data
},
    // 删除功能
    removeUserById(row){
      this.$confirm('确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        callback:async action => {
          if(action === 'confirm'){
            const { data: res } = await this.$http.delete('goods/'+row.goods_id)
            // console.log(res);
            if(res.meta.status !== 200){return this.$message.error('删除商品失败')}
            this.$message.success('删除商品成功')
            this.getGoodsList()
          }else{
            this.$message.info('取消删除')
          }
          
        },
      })
  
    },
    // 跳转到编辑页面
      goEditPage(row){
        this.$router.push('/edit'+'?id='+row.goods_id)
      },
      // 监听每页多少条 改变
     handleSizeChange(val) {
          this.queryInfo.pagesize = val;
      // console.log(`每页: ${val}条`);
      // 重新获取数据
      this.getGoodsList()
      },
    // 监听当前页 改变
      handleCurrentChange(val) {
        this.queryInfo.pagenum = val;
        // console.log(`当前页: ${val}`);
              this.getGoodsList()

      },
    
  }
}
</script>
<style lang="less" scoped>

</style>